<?php
class EngineAction extends Action
{	
	public function _init()
	{
		header("Content-Type:text/html; charset=utf-8");
		if(empty($_SESSION['uid']))
		{
			redirect("/liuk/index.php/?m=Index&a=index",2,"您还没登录,请先登录用户!");
		}		
	}
	
	//预加载
	public function preLoad()
	{
		//加载地图图片,背景音乐
		$sql  = "select id,terrainlayer,groundlayer,barrierlayer,music from __TABLE__";
		$list = M("Map")->query($sql);		
		if($list)
		{
			$len  = count($list); 
			for($i=0; $i<$len; $i++)
			{
				$json["map".$list[$i]['id']] = array(
					'id'		=>	$list[$i]['id'],
					'tl'		=>	$list[$i]['terrainlayer'],
					'gl'		=>	$list[$i]['groundlayer'],
					'bl'		=>	$list[$i]['barrierlayer'],
					'ms'		=>	$list[$i]['music']
				);
			}
		}
		//加载NPC图片
		$sql = "select id,avatar from __TABLE__";
		$npcList = M("Npc")->query($sql);
		if($npcList)
		{
			$npcLen = count($npcList);
			for($j=0; $j<$npcLen; $j++)
			{
				$json["npc".$npcList[$j]['id']] = array(
					'id'		=> $npcList[$j]['id'],
					'avatar'	=> $npcList[$j]['avatar']
				);
			}
		}
		
		//加载怪物图片
		$sql = "select id,avatar from __TABLE__";
		$monsterList = M("Monster")->query($sql);
		if($monsterList)
		{
			$monsterLen = count($monsterList);
			for($n=0; $n<$monsterLen; $n++)
			{
				$json["mon".$monsterList[$n]['id']] = array(
					'id'		=> $monsterList[$n]['id'],
					'avatar'	=> $monsterList[$n]['avatar']
				);
			}	
		}	
		
		//加载物品图片
		$sql = "select id,img from __TABLE__";
		$itemList = M("Item")->query($sql);
		if($itemList)
		{
			$itemLen = count($itemList);
			for($m=0; $m<$itemLen; $m++)
			{
				$json["ite".$itemList[$m]['id']] = array(
					'id'		=> $itemList[$m]['id'],
					'img'		=> $itemList[$m]['img']
				);
			}
		}
		//返回
		ajaxReturn($json);			
	}
	
	//获取地图信息
	public function getMap()
	{
		$info = $_POST;
		if(!empty($info))
		{
			$id	  = $info['mid'];
			$sql  = "select * from __TABLE__ where id=".$id;
			$list = M("Map")->query($sql);
			
			if($list)
			{
				//返回客户端
				$json = array(
					'id'	=> $list[0]['id'],
					'na'	=> $list[0]['name'],
					'tl'	=> $list[0]['terrainlayer'],
					'gl'	=> $list[0]['groundlayer'],
					'bl'	=> $list[0]['barrierlayer'],
					'ps'	=> $list[0]['points'],
					'mu'	=> $list[0]['music'],
					'is'	=> $list[0]['isclose']
				);
			}else {
				$json = array(
					'sign' => 1
				);
			}			
			ajaxReturn($json);
		}
	}
	
	//获取去NPC信息
	public function getNpc()
	{
		$info = $_POST;
		if(!empty($info))
		{
			$mid  = $info['mid'];
			$sql  = "select * from __TABLE__ where mid=".$mid;
			$list = M("Npc")->query($sql);
			
			if($list)
			{
				$json = null;
				$len  = count($list);
				for($j=0; $j<$len; $j++)
				{
					$json['id'.$j] = array(
						'id'			=> $list[$j]['id'],
						'mid'			=> $list[$j]['mid'],
						'name'		=> $list[$j]['name'],
						'posx'		=> $list[$j]['posx'],
						'posy'		=> $list[$j]['posy'],
						'avatar'		=> $list[$j]['avatar'],
						'tag'			=> $list[$j]['tag'],
						'needflag'	=> $list[$j]['needflag']
					);
				}	
			}else {
				$json = array(
					'sign'	=> 1
				);
			}
			ajaxReturn($json);
		}
	}
	
	//获取角色的信息
	public function getRole()
	{
		$uid = empty($_POST['uid']) ? $_SESSION['uid'] : $_POST['uid'];
		if(!empty($uid))
		{
			$role = M("Role");
			$role->load($uid);
			$json = array(
					'id'			=> $role->roleid,
					'mid'			=> $role->rolemid,
					'uid'			=> $role->roleuid,
					'tid'			=> $role->roletid,
					
					'name'		=> $role->name,
					'gender'		=> $role->gender,
					'faction'		=> $role->faction,
					'avatar'		=> $role->avatar,
					'level'		=> $role->level,
					'status'		=> $role->status,
					
					'lif'			=> $role->life,
					'exp'			=> $role->exprience,
					'mon'			=> $role->money,
					'att'			=> $role->attack,
					'spe'			=> $role->speed,
					'str'			=> $role->strength,
					'def'			=> $role->defend,
					
					'posx'		=> $role->posx,
					'posy'		=> $role->posy,
					'reposx'		=> $role->reposx,
					'reposy'		=> $role->reposy,
					
					'lastmove'	=> $role->lastmove,
					'online'		=> $role->online,
					'offline'		=> $role->offline	
			);	
			
		}else {
			$json = array( 'sign'	 => 0 );
		}
		ajaxReturn($json);
	}
	
	//
	public function getWaypoint()
	{
		$way = $_POST;
		if(!empty($way))
		{
			$mid = $way['mid'];
			$sql = "select * from liu_waypoint where midfrom=".$mid;
			$model = new Model();
			$list = $model->query($sql);
			if($list)
			{
				$json = null;
				$len  = count($list);
				for($i=0; $i<$len; $i++)
				{
					$json['way'.$list[$i]['id']] = array(
										'id'			=> $list[$i]['id'],
										'midfrom'		=> $list[$i]['midfrom'],
										'midto'		=> $list[$i]['midto'],
										'posxfrom'	=> $list[$i]['posxfrom'],
										'posyfrom'	=> $list[$i]['posyfrom'],
										'posxto'		=> $list[$i]['posxto'],
										'posyto'		=> $list[$i]['posyto'],
										'name'		=> $list[$i]['name']
								   );
				}
			}else {
				$json = array(
							'sign'	=> 0
						);
			}
			ajaxReturn($json);
		}
	}
	
	//
	public function getMonster()
	{
		$info = $_POST;
		if(!empty($info))
		{
			$mid 	= $info['mid'];
			$list 	= M("Monster")->getMonsterList($mid);
			if($list)
			{
				$json = $list;
			}else {
				$json = array( 'sign' => 0 );
			}
			ajaxReturn($json);
		}
	}
	
	//地图的切换
	public function changeMap()
	{
		$map = $_POST;
		if(!empty($map))
		{
			$uid	= $_SESSION['uid'];
			$mid 	= $map['mid'];
			$posx	= $map['posx'];
			$posy	= $map['posy'];
			$sql = "update __TABLE__ set mid={$mid},posx={$posx},posy={$posy} where uid={$uid}";
			$flag = M("Role")->execute($sql);
			if($flag)
			{
				$json = array( 'sign'	 => 1 );
			}else {
				$json = array( 'sign' => 0 );
			}
			ajaxReturn($json);
		}
	}
	
	//获取角色的信息
	public function getRoleInfo()
	{
		$roleid = $_SESSION['uid'];
		if(!empty($roleid))
		{
			$sql = "select id,rolename,avatar,level,lif,exp,mon,att,spe,str,def from __TABLE__ where uid=".$roleid;
			$info = M("Role")->query($sql);
			if($info)
			{				
				$left = "<div id='left-info'><p>名称:".$info[0]['rolename']."</p><p>等级:".$info[0]['level']."</p><p>金钱:".$info[0]['mon']."</p><p>经验:".$info[0]['exp']."</p></div>";
				$middle = "<div id='middle-info' style='background:url(/liuk/core/view/public/images/btn/".$info[0]['avatar'].") no-repeat'><p>生命:".$info[0]['lif']."</p></div>";
				$right = "<div id='right-info'><p>防御:".$info[0]['def']."</p><p>攻击:".$info[0]['att']."</p><p>速度:".$info[0]['spe']."</p><p>力量:".$info[0]['str']."</p></div>";
				$con = $left.$middle.$right;
				$json = array(
					'con'		=> $con
				);
			}else {
				$json = array( 'sign'	 => 0);
			}
		}else {
				$json = array( 'sign'	 => 0);
		}
		ajaxReturn($json);
	}

	//获取任务的信息
	public function getMission()
	{
		$rolesql = "select tid from __TABLE__ where uid=".$_SESSION['uid'];
		$roleInfo= M("Role")->query($rolesql);
		if($roleInfo)
		{
			$missionsql = "select * from __TABLE__ where id=".$roleInfo[0]['tid']." and roleid=".$_SESSION['uid'];
			$missionInfo= M("Rolemission")->query($missionsql);
			if($missionInfo)
			{
				//获取npc信息
				$npcsql = "select id,name,mid from __TABLE__ where id=".$missionInfo[0]['publicnpcid']." or id=".$missionInfo[0]['completenpcid'];
				$npcList = M("Npc")->query($npcsql);
				
				if($missionInfo[0]['publicnpcid'] == $missionInfo[0]['completenpcid'])
				{
					$name = $nextname = $npcList[0]['name'];
				}else {
					if($npcList[0]['id'] == $missionInfo[0]['publicnpcid'])
					{
						$name = $npcList[0]['name'];
						$nextname = $npcList[1]['name'];
					}else {
						$name = $npcList[1]['name'];
						$nextname = $npcList[0]['name'];
					}
				}
				
				//获取地图信息
				$mapsql = "select name from  __TABLE__ where id=".$missionInfo[0]['missionmid'];
				$mapList = M("Map")->query($mapsql);
				if($mapList)
				{
					$mapname = $mapList[0]['name'];
				}else {
					//$mapname = 0;
				}
				
				//获取奖励信息
				$awardsql = "select * from liu_npcaward where id=".$missionInfo[0]['awardid'];
				$model = new Model();
				$awardList = $model->query($awardsql);
				if($awardList)
				{
					$exp = $awardList[0]['exp'];
					$mon = $awardList[0]['mon'];
					$itemid  = $awardList[0]['itemid'];
					$itemnumber = $awardList[0]['itemnumber'];
				}
				
				//获取物品名称
				if($itemid)
				{
					$itemsql = "select name from  liu_item where id=".$itemid;
					$itemList = $model->query($itemsql);
					if($itemList)
					{
						$itemname = $itemList[0]['name'];
					}
				}				
				
				$json = array(
					'id'				=> $missionInfo[0]['id'],
					'eventid'			=> $missionInfo[0]['eventid'],
					'publicid'		=> $missionInfo[0]['publicnpcid'],
					'completeid'		=> $missionInfo[0]['completenpcid'],					
					'awardid'			=> $missionInfo[0]['missionmid'],
					'missionname'		=> $missionInfo[0]['missionname'],
					'missioninfo'		=> $missionInfo[0]['missioninfo'],
					'nextmissionid'	=> $missionInfo[0]['nextmissionid'],
					'type'			=> $missionInfo[0]['type'],
					'needid'			=> $missionInfo[0]['needid'],
					'publicname'		=> $name,
					'completename'	=> $nextname,
					'mapname'			=> $mapname,	
					'exp'				=> $exp,
					'mon'				=> $mon,
					'itemid'			=> $itemid,
					'itemnumber'		=> $itemnumber,
					'itemname'		=> $itemname,
					'isaccept'		=> 0
				);
				
				//完成任务完成所需的条件
				if($missionInfo[0]['needid'] != 0)
				{
					$needsql = "select * from liu_npcneed where id=".$mission[0]['needid'];
					$needList= $model->query($needsql);
					if($needList)
					{
						$needTmp = array(
							'nitemid'				=> $needList[0]['itemid'],
							'nitemnum'			=> $needList[0]['itemnumber'],
							'nmonster'			=> $needList[0]['monsterid'],
							'nmonsternum'			=> $needList[0]['monsternumber'],
							'nlevel'				=> $needList[0]['needlevel']
						);
						$json = array_merge($json,$needTmp);
					}
				}				
			}else {
				$json = array( 'sign' => 0);
			}
		}else {
			$json = array( 'sign' => 0);
		}	
		ajaxReturn($json);
	}
	
	//初始化事件
	public function loadEvent()
	{
		$e = $_POST;
		if(!empty($e))
		{
			$eid = $_POST['eid'];
			$sql = "select * from  __TABLE__ where eventid=".$eid;
			$eventList = M("Npcevent")->query($sql);
			if($eventList)
			{
				$npcsql = "select * from __TABLE__ where id=".$eventList[0]['npcid'];
				$npcList= M("Npc")->query($npcsql);
				if($npcList)
				{
					$name	= $npcList[0]['name'];
					$mid	= $npcList[0]['mid'];	
					$avatar	= $npcList[0]['avatar'];
				}
				
				$json = array(
					'id'					=> $eventList[0]['id'],
					'eventid'				=> $eventList[0]['eventid'],
					'nexteventid'			=> $eventList[0]['nexteventid'],
					'npcid'				=> $eventList[0]['npcid'],
					'nextnpcid'			=> $eventList[0]['nextnpcid'],
					'ismission'			=> $eventList[0]['ismission'],
					'npcwords'			=> $eventList[0]['npcwords'],
					'mywords'				=> $eventList[0]['mywords'],
					'callbackname'		=> $eventList[0]['callbackname'],
					'callbackfunction'	=> $eventList[0]['callbackfunction'],
					'npcname'				=> $name,
					'mid'					=> $mid,
					'avatar'				=> $avatar
				);
			}else {
				$json = array( 'sign' => 0 );
			}
		}else {
			$json = array( 'sign' => 1);
		}
		ajaxReturn($json);
	}
	
	//任务完成的奖励
	public function completeMission()
	{
		$a = $_POST;
		if(!empty($a))
		{
			$awardid = $a['aid'];
			$awardsql = "select * from __TABLE__ where id=".$awardid;
			$awardList= M("Npcaward")->query($awardsql);
			if($awardList) //itermid,itermnumber,exp,mon
			{
				$rolesql = "update __TABLE__ set exp=exp+".$awardList[0]['exp'].", mon=mon+".$awardList[0]['mon']." where uid=".$_SESSION['uid'];
				$signRole = M("Role")->execute($rolesql);
				
				$goodssql = "select * from __TABLE__ where iid=".$awardList[0]['itemid']." and rid=".$_SESSION['uid'];	
				$goodsList= M("Rolegoods");
				$list = $goodsList->query($goodssql);
				if($list)
				{
					$goodssql2 = "update __TABLE__ set number=number+".$awardList[0]['itemnumber']." where iid=".$awardid;
				}else {
					$goodssql2 = "insert into __TABLE__ values('',".$_SESSION['uid'].",".$awardid.",".$awardList[0]['itemnumber'].")";
				}
				$signGoods = $goodsList->execute($goodssql2);			
			}			
		}
		ajaxReturn($json);	
	}
	
	public function load()
	{
		$role = M("Role");
		$list = $role->load(2);
		dump($role->roleid . '|' .$role->rolemid);
	}
	
	//发布新的任务
	public function publicMission()
	{
		$mission = $_POST;
		if(!empty($mission))
		{
			$missionid = $mission['nextmid'];
			$missionsql = "select * from __TABLE__ where id=".$missionid;
			$missionInfo= M("Rolemission")->query($missionsql);
			if($missionInfo)
			{
				//获取npc信息
				$npcsql = "select id,name,mid from __TABLE__ where id=".$missionInfo[0]['publicnpcid']." or id=".$missionInfo[0]['completenpcid'];
				$npcList = M("Npc")->query($npcsql);
				
				if($missionInfo[0]['publicnpcid'] == $missionInfo[0]['completenpcid'])
				{
					$name = $nextname = $npcList[0]['name'];
				}else {
					if($npcList[0]['id'] == $missionInfo[0]['publicnpcid'])
					{
						$name = $npcList[0]['name'];
						$nextname = $npcList[1]['name'];
					}else {
						$name = $npcList[1]['name'];
						$nextname = $npcList[0]['name'];
					}
				}
				
				//获取地图信息
				$mapsql = "select name from  __TABLE__ where id=".$missionInfo[0]['missionmid'];
				$mapList = M("Map")->query($mapsql);
				if($mapList)
				{
					$mapname = $mapList[0]['name'];
				}else {
					//$mapname = 0;
				}
				
				//获取奖励信息
				$awardsql = "select * from liu_npcaward where id=".$missionInfo[0]['awardid'];
				$model = new Model();
				$awardList = $model->query($awardsql);
				if($awardList)
				{
					$exp = $awardList[0]['exp'];
					$mon = $awardList[0]['mon'];
					$itemid  = $awardList[0]['itemid'];
					$itemnumber = $awardList[0]['itemnumber'];
				}
				
				//获取物品名称
				if($itemid)
				{
					$itemsql = "select name from  liu_item where id=".$itemid;
					$itemList = $model->query($itemsql);
					if($itemList)
					{
						$itemname = $itemList[0]['name'];
					}
				}
				
				//获取对应的NPC对白信息
				$npceventsql = "select npcwords,mywords,callbackname from __TABLE__ where eventid=".$missionInfo[0]['eventid'];	
				$eventInfo	 = M("Npcevent")->query($npceventsql);
				if($eventInfo)
				{
					$npcwords = $eventInfo[0]['npcwords'];
					$mywords  = $eventInfo[0]['mywords'];	
					$callbackname = $eventInfo[0]['callbackname'];
				}	
				
				$json = array(
					'id'				=> $missionInfo[0]['id'],
					'roleid'			=> $missionInfo[0]['roleid'],
					'eventid'			=> $missionInfo[0]['eventid'],
					'publicnpcid'		=> $missionInfo[0]['publicnpcid'],
					'completenpcid'	=> $missionInfo[0]['completenpcid'],
					'awardid'			=> $missionInfo[0]['awardid'],
					'missionmid'		=> $missionInfo[0]['missionmid'],
					'needid'			=> $missionInfo[0]['needid'],
					'missionname'		=> $missionInfo[0]['missionname'],
					'missioninfo'		=> $missionInfo[0]['missioninfo'],
					'type'			=> $missionInfo[0]['type'],
					'nextmissionid'	=> $missionInfo[0]['nextmissionid'],
					'publicname'		=> $name,
					'completename'	=> $nextname,
					'mapname'			=> $mapname,
					'exp'				=> $exp,
					'mon'				=> $mon,
					'itemid'			=> $itemid,
					'itemname'		=> $itemname,
					'itemnumber'		=> $itemnumber,
					'isaccept'		=> 0,
					'npcwords'		=> $npcwords,
					'mywords'			=> $mywords,
					'callbackname'	=> $callbackname						
				);
				
				//完成任务完成所需的条件
				if($missionInfo[0]['needid'] != 0)
				{
					$needsql = "select * from liu_npcneed where id=".$missionInfo[0]['needid'];
					$needList= $model->query($needsql);
					if($needList)
					{
						$needTmp = array(
							'nitemid'				=> $needList[0]['itemid'],
							'nitemnum'			=> $needList[0]['itemnumber'],
							'nmonster'			=> $needList[0]['monsterid'],
							'nmonsternum'			=> $needList[0]['monsternumber'],
							'nlevel'				=> $needList[0]['needlevel']
						);
						$json = array_merge($json,$needTmp);
					}
				}				
			}else {
				$json = array( 'sign' => 0);
			}
			ajaxReturn($json);
		}
	}
	
	//获取玩家打败的列表
	public function getBeat()
	{
		$beatsql = "select * from liu_rolebeat where rid=".$_SESSION['uid'];
		$model = new Model();
		$beatList = $model->query($beatsql);
		if($beatList)
		{
			$beat = array();
			$len = count($beatList);
			for($i=0; $i<$len; $i++)
			{
				$beat["m".$i] = $beatList[$i];
			}
			$json = $beat;
		}else {
			$json = array( 'sign'=> 0);
		}
		ajaxReturn($json);
	}
	
	//获取物品信息
	public function getGoods()
	{
		$sql = "select * from __TABLE__ where rid=".$_SESSION['uid'];
		$goodsList = M("Rolegoods")->query($sql);
		if($goodsList)
		{
			$len = count($goodsList);
			$item= M("Item");
			$tmp = array();
			$equitList 	= array();
			$itemList	= array();
			for($i=0; $i<$len; $i++)
			{
				$itemsql = "select * from __TABLE__ where id=".$goodsList[$i]['iid'];				
				$tmp = $item->query($itemsql);				
				$itemList["it".$i] = $tmp[0];
				$itemList["it".$i]['number'] = $goodsList[$i]['number'];		
			}
			$json = $itemList  ? $itemList : array( 'sign' => 0 );			
		}else {
			$json = array( 'sign' => 0 );
		}
		ajaxReturn($json);
	}
	
	//获取role技能
	public function getSkill()
	{
		$skillsql = "select sid from __TABLE__ where rid=".$_SESSION['uid'];
		$skillList= M("Roleskill")->query($skillsql);
		if($skillList)
		{
			$len = count($skillList);
			for($i=0; $i<$len; $i++)
			{
				$sksql= "select * from __TABLE__ where id=".$skillList[$i]['sid'];
				$skillInfo = M("Skill")->query($sksql);
				if($skillInfo)
				{
					$json['sk'.$skillList[$i]['sid']] = $skillInfo[0];
				}
			}
		}else {
			$json = array( 'sign' => 0 );
		}
		ajaxReturn($json);
	}
	//获取role技能
	public function getAllSkill()
	{
		$skillsql = "select * from __TABLE__";
		$skillList= M("Skill")->query($skillsql);
		if($skillList)
		{
			$len = count($skillList);
			for($i=0; $i<$len; $i++)
			{
				$json['sk'.$skillList[$i]['id']] = $skillList[$i];
			}
		}else {
			$json = array( 'sign' => 0 );
		}
		ajaxReturn($json);
	}
	
	//加载其他玩家信息
	public function loadRoles()
	{
		$info = $_POST;
		if(!empty($info['mid']))
		{
			$mid = $info['mid'];
			$sql = "select * from __TABLE__ where mid=".$mid." and status=1 and uid!=".$_SESSION['uid'];
			$roleList = M("Role")->query($sql);
			if($roleList)
			{
				$json = array();
				$len = count($roleList);
				for($i=0; $i<$len; $i++)
				{
					$json[$roleList[$i]['id']] = array(
							'id'			=> $roleList[$i]['id'],
							'mid'			=> $roleList[$i]['mid'],
							'uid'			=> $roleList[$i]['uid'],
							'tid'			=> $roleList[$i]['tid'],
							
							'name'		=> $roleList[$i]['rolename'],
							'gender'		=> $roleList[$i]['gender'],
							'faction'		=> $roleList[$i]['faction'],
							'avatar'		=> $roleList[$i]['avatar'],
							'level'		=> $roleList[$i]['level'],
							'status'		=> $roleList[$i]['status'],
							
							'lif'			=> $roleList[$i]['lif'],
							'exp'			=> $roleList[$i]['exp'],
							'mon'			=> $roleList[$i]['mon'],
							'att'			=> $roleList[$i]['att'],
							'spe'			=> $roleList[$i]['spe'],
							'str'			=> $roleList[$i]['str'],
							'def'			=> $roleList[$i]['def'],
							
							'posx'		=> $roleList[$i]['posx'],
							'posy'		=> $roleList[$i]['posy'],
							'reposx'		=> $roleList[$i]['reposx'],
							'reposy'		=> $roleList[$i]['reposy'],
							
							'lastmove'	=> $roleList[$i]['lastmove'],
							'online'		=> $roleList[$i]['online'],
							'offline'		=> $roleList[$i]['offline']
					);	
				}//[for] -> end
			}else {
				$json = array( 'sign' => 0 );
			}//[IF:$roleList] -> end					
		}else {
			$json = array( 'sign' => 0 );
		}
		ajaxReturn($json);
	}
	
	//主方法
	public function index()
	{
		$this->display();
	}
	
	public function test()
	{
		$npcsql = "select id,name from __TABLE__ where id=5 or id=5";
		$npcList = M("Npc")->query($npcsql);
		dump($npcList);
	}
	
}
?>